﻿using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Text;
using Utils.Weixin.Pay.Result;
using static System.Net.Mime.MediaTypeNames;

namespace Utils.Weixin.Pay.Notify
{
    public class NotifyResult
    {
        [JsonProperty("resource_type")]
        public string ResourceType { get; set; }

        [JsonProperty("event_type")]
        public string EventType { get; set; }

        [JsonProperty("summary")]
        public string Summary { get; set; }

        [JsonProperty("resource")]
        public ResourceResult Resource { get; set; }

        /// <summary>
        /// 退款
        /// </summary>
        public bool IsRefund => EventType.StartsWith("REFUND.");

        /// <summary>
        /// 订单支付
        /// </summary>
        public bool IsTransaction => EventType.StartsWith("TRANSACTION.");

        public bool IsSuccess => EventType.EndsWith(".SUCCESS");


        private JObject _decryptResource;

        public string JSONText { get; set; }

        internal void Decrypt(string apiV3)
        {
            JSONText = Security.AESHelper.AesGcmDecrypt(apiV3, Resource.AssociatedData, Resource.Nonce, Resource.Ciphertext);
            _decryptResource = JObject.Parse(JSONText);
        }

        public TransactionResult Transaction => TransactionResult.Create(_decryptResource);


        public RefundResult Refund => RefundResult.Create(_decryptResource);

    }
}
